package cn.krund.lyc.hotlerobot.Ros.service.communicationinfo;

import cn.krund.lyc.hotlerobot.utils.DataConversionUtil;

/**
 * @包名: cn.krund.lyc.hotlerobot.robot.service.communicationinfo
 * @作者: 刘育成
 * @时间: 2018/4/2  15:23
 * @org: 克路德人工智能
 * @描述: TODO  ros协议信息
 * @fileName: RosProtocolInfo
 */

public class RosProtocolInfo {

    private byte[] head;
    private int len;
    private byte ver;
    private byte ack;
    private byte seq;
    private byte crc16;
    private byte[] data;
    private byte datacrc;

    public byte[] getHead() {
        return head;
    }

    public void setHead(byte[] head) {
        this.head = head;
    }

    public int getLen() {
        return len;
    }

    public void setLen(int len) {
        this.len = len;
    }

    public byte getVer() {
        return ver;
    }

    public void setVer(byte ver) {
        this.ver = ver;
    }

    public byte getAck() {
        return ack;
    }

    public void setAck(byte ack) {
        this.ack = ack;
    }

    public byte getSeq() {
        return seq;
    }

    public void setSeq(byte seq) {
        this.seq = seq;
    }

    public byte getCrc16() {
        return crc16;
    }

    public void setCrc16(byte crc16) {
        this.crc16 = crc16;
    }

    public byte getDatacrc() {
        return datacrc;
    }

    public void setDatacrc(byte datacrc) {
        this.datacrc = datacrc;
    }

    public RosProtocolInfo(){
        this.setHead(new byte[]{(byte)0xAA});
    }
    public int size(){
        int ret = this.getHead().length;
        ret+=this.getLen();
        ret+=6;
        return ret;
    }


    public synchronized void calcChkSum(){
        byte chk=0;
        for (byte b:this.getData()
                ) {
            chk+=b;
        }
        this.setDatacrc(chk);
    }
    public byte[] toBytes(){
        byte[] ret = new byte[this.size()];
        System.arraycopy(this.getHead(), 0, ret, 0, this.getHead().length);
//        ret[2]=this.getType();
//        ret[3]=this.getCmd();
        System.arraycopy(DataConversionUtil.reverse(DataConversionUtil.int2Bytes(this.getLen())), 0, ret, 4, 4);
        System.arraycopy(this.getData(), 0, ret, 8, this.getData().length);
        ret[ret.length-1]=this.getDatacrc();
        return ret;
    }



    public byte[] getData() {
        return data;
    }

    public void setData(byte[] datas) {
        this.data = datas;
        int len =datas.length+1;
        this.setLen(len);

        this.calcChkSum();
    }
}
